package com.ohuang.apkpatch

import org.dom4j.Document
import org.dom4j.io.OutputFormat
import org.dom4j.io.SAXReader
import org.dom4j.io.XMLWriter
import java.io.File
import java.io.FileWriter

/**
 * 修改id 0x7f->0x80
 *
 */
fun replacePublicXmlId(path: String, idHeader: String = "0x7f", replace: String = "0x80") {
    var saxReader = SAXReader()
    var read = saxReader.read(path)
    var rootElement = read.rootElement

    var elements = rootElement.elements()

    elements.forEach {
        var id = it.attribute("id").data as String


        var replace1 = id.replace(idHeader, replace)
        it.setAttributeValue("id", replace1)
    }

    saveXml(path, read)
}

/**
 * 修改id
 * 0x7f010000  -> 0x7f810000
 */
fun publicXmlIdPlus(path: String) {
    var saxReader = SAXReader()
    var read = saxReader.read(path)
    var rootElement = read.rootElement

    var elements = rootElement.elements()

    elements.forEach {
        var id = it.attribute("id").data as String
        var ox16ToInt = ox16ToInt(id)
        var and1 = ox16ToInt.and(0xff00ffff.toInt())

        var t = ox16ToInt.and(0x00ff0000.toInt()).shr(16) + 0x80
        var num = t.shl(16).or(and1)
        var intTo0X16 = intTo0X16(num)

        it.setAttributeValue("id", intTo0X16)
    }

    saveXml(path, read)
}

fun readPublic(path: String): HashMap<String, HashMap<String, String>> {
    var saxReader = SAXReader()
    var read = saxReader.read(path)
    var rootElement = read.rootElement

    var elements = rootElement.elements()
    var hashMap = HashMap<String, HashMap<String, String>>()
    elements.forEach {
        var type = it.attribute("type").data as String
        var name = it.attribute("name").data as String
        var id = it.attribute("id").data as String

        if (hashMap.containsKey(type)) {
            var map = hashMap[type]
            if (map == null) {
                var map = HashMap<String, String>()
                map[name] = id
                hashMap[type] = map
            } else {
                map[name] = id
            }
        } else {
            var map = HashMap<String, String>()
            map[name] = id
            hashMap[type] = map

        }
    }

    return hashMap
}

/**
 * 没有更新资源还原老的id
 */
fun restartPublicOldId(oldPath: String, newPath: String, outPath: String, needUpdate: Map<String, Set<String>>) {
    var readPublic = readPublic(oldPath)
    var saxReader = SAXReader()
    var read = saxReader.read(newPath)
    var rootElement = read.rootElement

    var elements = rootElement.elements()
    elements.forEach {
        var type = it.attribute("type").data as String
        var name = it.attribute("name").data as String
        var id = it.attribute("id").data as String
        if (needUpdate.containsKey(type)) {
            var strings = needUpdate[type]
            if (strings!!.contains(name)) {
                return@forEach
            }
        }
        if (readPublic.containsKey(type)) {
            var hashMap = readPublic[type]
            if (hashMap!!.containsKey(name)) {
                var s = hashMap[name]
                it.setAttributeValue("id", s)
            }
        }
    }
    saveXml(outPath, read)
}

fun saveXml(path2: String, read1: Document?) {
    try {
        var createPrettyPrint = OutputFormat.createPrettyPrint()

        createPrettyPrint.encoding = "utf-8"
        var writer = XMLWriter(FileWriter(path2), createPrettyPrint);
        writer.write(read1)
        writer.close()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

/***
 * 生成补丁public.xml
 */
fun replaceOldId(patchResPath:String,oldPublic:String,newPublic:String,outputPublic:String){
    var readRes = readRes(patchResPath)
    restartPublicOldId(oldPublic,newPublic,outputPublic,readRes)
}



fun readRes(path: String): Map<String, Set<String>> {
    var map = HashMap<String, HashSet<String>>()
    var file = File(path)
    file.listFiles()?.forEach { it ->

        if (it.isDirectory) {
            var split = it.name.split("-")
            var s = split[0]
            var set: HashSet<String>? = null
            if (map.containsKey(s)) {
                set = map[s]

            } else {
                set = HashSet<String>()
                map[s] = set
            }
            it.listFiles()?.forEach { it2 ->
                if (it2.isFile) {
                    set?.add(it2.name.split(".")[0])
                }

            }
        }
    }
    return map
}